//
//  GDColor.m
//  GDFramework
//
//  Created by David Thorpe on 2007-07-22.
//  Copyright 2007 David Thorpe. All rights reserved.
//

#import <gd.h>
#import "GDColor.h"


@implementation GDColor

@synthesize index = m_theIndex;

- (id)initWithPtr:(void *)theImagePtr index:(NSInteger)theIndex {
	self = [super init];
	if (self != nil) {
		if (theIndex < 0 || theImagePtr == nil) {
			[self release];
			return nil;
		}
		m_theIndex = theIndex;
		m_theImagePtr = theImagePtr;
	}
	return self;
}

- (gdImagePtr)imagePtr {
	return (gdImagePtr)m_theImagePtr;
}

- (CGFloat)redComponent {
	return ((CGFloat)gdImageRed([self imagePtr], [self index])) / 255.0;
}

- (CGFloat)greenComponent {
	return ((CGFloat)gdImageGreen([self imagePtr], [self index])) / 255.0;
}

- (CGFloat)blueComponent {
	return ((CGFloat)gdImageBlue([self imagePtr], [self index])) / 255.0;
}

- (CGFloat)alphaComponent {
	return 1.0 - ((CGFloat)gdImageAlpha([self imagePtr], [self index])) / 127.0;
}

- (NSString *)description {
	return [NSString stringWithFormat:@"<GDColor %f,%f,%f,%f>",
			[self redComponent], [self greenComponent],
			[self blueComponent], [self alphaComponent]];
}

@end